﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DrawableObjects.Actors.Weapons;

namespace DrawableObjects.Actors.Aircrafts.Controller
{
    /// <summary>
    /// A controller managing player controlled aircrafts.
    /// </summary>
    public class PlayerControlledAircraft : AircraftController
    {
        public float rotateZ;
        private float rotationSpeed = 0.05f;
        private float maxRotation = MathHelper.PiOver4/2;

        /// <summary>
        /// Checks for key inputs and causes the aircraft that is controlled to act accordingly.
        /// </summary>
        /// <param name="newKeyboardState">The most recently cached keyboard state,</param>
        public void Update(KeyboardState newKeyboardState)
        {
            if (this.aircraft != null)
            {
                Vector2 direction = Vector2.Zero;
                if (newKeyboardState.IsKeyDown(AircraftCommand.MOVE_FORWARD.standardKey)) 
                {
                    direction = Vector2.Add(direction, new Vector2(0, -1));
                }
                if (newKeyboardState.IsKeyDown(AircraftCommand.MOVE_BACK.standardKey))
                {
                    direction = Vector2.Add(direction, new Vector2(0, 1));
                }
                if (newKeyboardState.IsKeyDown(AircraftCommand.MOVE_LEFT.standardKey))
                {                    
                    direction = Vector2.Add(direction, new Vector2(-1, 0));
                    
                    if (!(rotateZ < -maxRotation))
                    {
                        rotateZ -= rotationSpeed;
                    }
                }
                if (newKeyboardState.IsKeyDown(AircraftCommand.MOVE_RIGHT.standardKey))
                {
                    direction = Vector2.Add(direction, new Vector2(1, 0));

                    if (!(rotateZ > maxRotation))
                    {
                        rotateZ += rotationSpeed;
                    }

                }
                if (newKeyboardState.IsKeyDown(AircraftCommand.FIRE.standardKey))
                {
                    aircraft.Fire(new Vector2(0,-1));
                }
                if (!(newKeyboardState.IsKeyDown(AircraftCommand.MOVE_RIGHT.standardKey) | newKeyboardState.IsKeyDown(AircraftCommand.MOVE_LEFT.standardKey)))
                {
                    if (rotateZ > 0)
                    {
                        rotateZ -= rotationSpeed;
                    }
                    if (rotateZ < 0)
                    {
                        rotateZ += rotationSpeed;
                    }
                }
                aircraft.standardRotation = Matrix.CreateRotationZ(rotateZ);

                aircraft.moveInDirection(direction);
            }
        }

        public override void Update()
        {
        }

        public PlayerControlledAircraft(Aircraft aircraft)
        {
            this.aircraft = aircraft;
        }

    }
}

